<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class UserFavoriteModel extends Model
{
    protected $table ='user_favorite';
    public $timestamps = false;
    protected $guarded = [];

    public function getFans($uuid, $toid)
    {

        if(is_null($toid) || $toid == 'undefined'){
            $result = [];
            $data = UserFavoriteModel::where(['uid' => $uuid]) ->get(); //得到所有cid
            foreach ($data as $key => $value){
                $result[$key] = $value -> cid;
            }

            if(!is_null($data)){

                $res = CardModel::
                whereIn('id',$result) ->
                withCount(['comment']) ->
                withCount(['cardLike']) ->
                with([
                    'comment' => function($query){
                        $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                    },
                    'image' =>function($query)  {
                        $query ->select(['id','card_id','img'])  ->get();
                    },
                    'cardLike' => function($query) {
                        $query -> select(['id','cid']) ->first() ;
                    }

                ]) -> where(['is_delete' => 0])    -> paginate(10);
                if($res){
                    return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
                }else{
                    return ['code' => false,'data' => '数据为空'];
                }

            }
            return ['code' => false,'data' => '数据为空'];

        }else{
            $result = [];
            $data = UserFavoriteModel::where(['uid' => $toid]) ->get(); //得到所有cid
            foreach ($data as $key => $value){
                $result[$key] = $value -> cid;
            }

            if(!is_null($data)){

                $res = CardModel::
                whereIn('id',$result) ->
                withCount(['comment']) ->
                withCount(['cardLike']) ->
                with([
                    'comment' => function($query){
                        $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                    },
                    'image' =>function($query)  {
                        $query ->select(['id','card_id','img'])  ->get();
                    },
                    'cardLike' => function($query) {
                        $query -> select(['id','cid']) ->first() ;
                    }

                ]) -> where(['is_delete' => 0])    -> paginate(10);
                if($res){
                    return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
                }else{
                    return ['code' => false,'data' => '数据为空'];
                }

            }
            return ['code' => false,'data' => '数据为空'];

        }

    }
}
